package com.antd.auth.handler;

import cn.hutool.core.collection.CollUtil;
import com.antd.common.auth.security.AntdUserDetails;
import com.antd.common.log.annotation.SystemLog;
import com.antd.common.log.enums.LogType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2020/11/19 10:31
 */
@Slf4j
@Component
public class AntdAuthenticationSuccessEventHandler implements ApplicationListener<AuthenticationSuccessEvent> {
    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        Authentication authentication = (Authentication) event.getSource();
        if (CollUtil.isNotEmpty(authentication.getAuthorities())) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof AntdUserDetails){
                log.info("用户：{} 登录成功",((AntdUserDetails) principal).getUsername());
            }
        }
    }
}
